loadInitData();
function loadInitData() {
    var token = localStorage.getItem("token");
    console.log("token:"+token);
    $.ajax({
        type: "get",
        url: "/demo/XQing",
        headers: {'token': token},
        success: function (result) {
            var loginCustomer=result.data;
            if(loginCustomer!=null){
                var customerId = loginCustomer.customerId;
                var url = "/demo/order?customerId=" + customerId;
                $(".head-user").hide();
                $(".user-menu").html('<li class="text"><a href='+url+'>个人中心</a></li>\n' +
                    '                 <li class="text" id="return"><button style="border: none">退出登录</button></li>');
                $(".user-info").show();
            }else {
                $(".head-user").show();
                $(".user-info").hide();
            }
            $("#return").click(function (){
                var token = localStorage.removeItem("token");
                window.location.href = "http://localhost:8080/demo/index.jsp";
            })
        }
    })
}
loadInit();
function loadInit() {
    var movieTicketInfo = localStorage.getItem("movieTicketInfo");
    var movieTicketNum = localStorage.getItem("movieTicketNum");
    window.orderDate = localStorage.getItem("orderDate");
    localStorage.removeItem("movieTicketInfo");
    localStorage.removeItem("movieTicketNum");
    localStorage.removeItem("orderDate");
    console.log(movieTicketInfo);
    console.log(movieTicketNum);
    //杰杰影院,一号厅,中文,今天  11月4日 15:40,哥，你好,120,1
    //4排1座,4排2座
    window.str=movieTicketInfo.split(",");
    $(".information").html('<td class="movie-name" >《'+str[4]+'》</td>\n' +
        '                    <td class="showtime" >'+str[3]+'</td>\n' +
        '                    <td class="cinema-name"  >'+str[0]+'</td>\n' +
        '                    <td>\n' +
        '                        <span class="hall" >'+str[1]+'</span>\n' +
        '                        <div class="seats">\n' +
        '                            <div>\n' +
        '                                <span>'+movieTicketNum+'</span>\n' +
        '                            </div>\n' +
        '                        </div>\n' +
        '                    </td>');
    $(".prices").text(str[5])
    $(".aaa").text(str[5])
    $(".total").text("购票总价 :"+str[5])
    $(".pre").text("优惠金额 :"+"0")
    $(".form_info").html('<input type="hidden" name="customerId" value='+str[7]+'>\n' +
        '                <input type="hidden" name="scheduleId" value='+str[6]+'>\n' +
        '                <input type="hidden" name="orderAdjustedPrice" value='+str[5]+'>\n' +
        '                <input type="hidden" name="orderSeatNum" value='+movieTicketNum+'>\n' +
        '                <input type="hidden" name="customerAndCouponId" value='+"0"+'>\n' +
        '                <input class="submitBtn" type="submit" value="前往支付宝进行支付">');

}
console.log(orderDate)
//支付剩余时间倒计时
$(function (){  //orderDate是订单生效时间，后台的Date类型传到前端成了毫秒
    var now=new Date();
    var end=new Date(Number(orderDate)+10*60*1000);  //提交订单的时间+5min就是订单失效时间，后台Date传过来变成了毫秒
    console.log("end:"+end)
    /*两个时间相减,得到的是毫秒ms,变成秒*/
    var result=Math.floor(end-now)/1000; //result= 30*60s;
    var interval=setInterval(sub,1000); //定时器 调度对象
    /*封装减1秒的函数*/
    function sub(){
        if (result>1) {
            result = result - 1;
            var second = Math.floor(result % 60);     // 计算秒 ，取余
            var minite = Math.floor((result / 60) % 60); //计算分 ，换算有多少分，取余，余出多少秒
            //var hour = Math.floor(result/1000/60/60%24);  //计算小时，这里最长只有30min，用不着
            //var day = Math.floor(result/1000/60/60/24);   //计算天，这里最长只有30min，用不着
            //console.log("333333")
            $("#remainTime").html('<p class="time-left">\n' +
                '                    请在\n' +
                '                    <span class="minute">'+minite+'</span>\n' +
                '                    分钟\n' +
                '                    <span class="second">'+second+'</span>秒内完成支付\n' +
                '                </p>\n' +
                '                <p class="tip">超时订单会自动取消，如遇支付问题，请致电猫眼客服：1010-5335</p>');
        } else{
            //这里可以添加倒计时结束后需要执行的事件
            alert("未在规定时间内支付，订单已失效！");
            window.clearInterval(interval);
            //TODO 订单失效后的一些列操作：更改订单状态的同时回库存
            window.location.href = "http://localhost:8080/demo/MovieSeat?scheduleId="+str[6];
        }
    }
});
getConfirmCoupon();
function getConfirmCoupon(){
    var token = localStorage.getItem("token");
    $.ajax({
        type: "get",
        url: "/demo/confirmCoupon",
        headers: {'token': token},
        success: function (result) {
            // console.log(result.length)
            console.log("======================================"+result.custandcouponId)
            console.log("======================================"+result.couponList[0].couponName)
            $(".myCoupon").html("");
            $(".myCoupon").append('<option>----请选择你的优惠券----</option>');
            for (var i=0;i<result.custandcouponId.length;i++){
                $(".myCoupon").append('<option class="option"><div><span class="couponId" style="display: none !important;">'+
                    result.custandcouponId[i]+'</span></div>' +
                    '<span class="couponName">'+
                    result.couponList[i].couponName+
                    '</span></option>')


            }
            for (var i=0;i<result.length;i++) {
                var coupon =   document.querySelectorAll(".couponId")[i];
                console.log("第"+i+"个 coupon id=" + coupon);

                document.querySelectorAll(".couponId")[i].style.display="none";
            }


        }
    })

}


// 选择优惠券
$(".myCoupon").change(function (){

    var selectedCoupon = $(".myCoupon  option:selected").text();
    console.log("选中的元素是：" + selectedCoupon);
    var textNum1 = parseInt(selectedCoupon.charAt(2))*10+parseInt(selectedCoupon.charAt(3));
    var textNum2 = parseInt(selectedCoupon.charAt(5))*10+parseInt(selectedCoupon.charAt(6));
    var couponId = selectedCoupon.charAt(0);
    console.log("优惠券id:"+couponId);
    var a = document.querySelector('.aaa').textContent;
    var totalPrice =parseFloat(a);
    if (textNum1<totalPrice){
        document.getElementsByName('customerAndCouponId')[0].value=couponId;
        console.log(document.getElementsByName('customerAndCouponId')[0].value);
        var price = totalPrice-textNum2
        document.getElementsByName('orderAdjustedPrice')[0].value=price;
        console.log("实际支付金额:"+price);
        document.querySelector('.price.prices').textContent = price.toString();
        document.querySelector('.total').textContent= "购票总价 :"+totalPrice.toString();
        document.querySelector('.pre').textContent = "优惠金额 :"+textNum2.toString();
    }else {
        document.getElementsByName('customerAndCouponId')[0].value='1';
    }
})
/*
document.querySelector(".myCoupon").onclick=function(event){
    var element=event.target;//事件作用的元素
    var nodename=element.nodeName;//标签名称
    console.log("作用元素:"+element);
    console.log("标签名称:"+nodename);
    console.log(element.textContent);
    var text = element.textContent;
    var textNum1 = parseInt(text.charAt(1))*10+parseInt(text.charAt(2));
    var textNum2 = parseInt(text.charAt(4))*10+parseInt(text.charAt(5));
    var couponId = document.querySelector('.c');
    console.log("优惠券id:"+couponId);
    console.log("====================="+document.querySelector('.price.prices'))
    var a = document.querySelector('.price.prices').textContent;
    var totalPrice =parseFloat(a);
    if (textNum1<totalPrice){
        document.getElementsByName('customerAndCouponId')[0].value='couponId';
        var price = totalPrice-textNum2;
        console.log("实际支付金额:"+price);
        document.querySelector('.price.prices').textContent = price.toString();
        document.querySelector('.total').textContent= "购票总价 :"+totalPrice.toString();
        document.querySelector('.pre').textContent = "实际支付 :"+textNum2.toString();
    }else {
        document.getElementsByName('customerAndCouponId')[0].value='1';
    }
}*/
